﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "OneWayLinkList.h"

struct PersonList
{
	char name[64];
	int age;
};

void myPrintList(void* data)
{
	struct PersonList* person = (struct PersonList*)data;
	printf("18.输出链表中的元素person的Name：%s  Age：%d\n", person->name, person->age);
}

int myCompareList(void* d1, void* d2)
{
	struct PersonList* p1 = (struct PersonList*)d1;
	struct PersonList* p2 = (struct PersonList*)d2;

#if 0
	if (strcmp(p1->name, p2->name) == 0 && p1->age == p2->age)
	{
		return 1;
	}
	return 0;
#endif
	return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}

void testOneWayLinkList()
{
	//15.创建链表；
	LinkList list = Init_OneWayLinkList();

	//16.创建数据；
	struct PersonList p1 = { "aaa",10 };
	struct PersonList p2 = { "bbb",20 };
	struct PersonList p3 = { "ccc",30 };
	struct PersonList p4 = { "ddd",40 };
	struct PersonList p5 = { "eee",50 };
	struct PersonList p6 = { "fff",60 };
	struct PersonList p7 = { "ggg",70 };

	//17.插入数据；
	Insert_OneWayLinkList(list, 0, &p1);//10
	Insert_OneWayLinkList(list, 0, &p2);//20,10
	Insert_OneWayLinkList(list, 1, &p3);//20,30,10
	Insert_OneWayLinkList(list, 2, &p4);//20,30,40,10
	Insert_OneWayLinkList(list, 20, &p5);//20,30,40,10,50
	Insert_OneWayLinkList(list, 3, &p6);//20,30,40,60,10,50
	Insert_OneWayLinkList(list, 6, &p7);//20,30,40,60,10,50,70

	Foreach_OneWayLinkList(list, myPrintList);//20,30,40,60,10,50,70

	printf("---------------------------------\n");
	printf("37.输出单向链表OneWayLinkList的Size为：%d\n", Size_OneWayLinkList(list));//7
	RemoveByPos_OneWayLinkList(list, 3);
	printf("-----------38.输出删除索引为3的元素节点后的单向链表：--------------\n");
	Foreach_OneWayLinkList(list, myPrintList);//20,30,40,10,50,70

	struct PersonList pDelPerson = { "ggg",70 };
	RemoveByVal_OneWayLinkList(list, &pDelPerson, myCompareList);
	printf("-----------39.输出删除元素节点值为pDelPerson后的单向链表：--------------\n");
	Foreach_OneWayLinkList(list, myPrintList);//20,30,40,10,50

	//40.清空链表；
	Clear_OneWayLinkList(list);
	printf("-----------41.清空链表后，输出链表的大小：----------------------\n");
	printf("37.输出单向链表OneWayLinkList的Size为：%d\n", Size_OneWayLinkList(list));//0

	//42.销毁链表；
	Destroy_OneWayLinkList(list);
}

//main222：单向链表初始化插入遍历删除大小清空销毁
int main222()
{
	testOneWayLinkList();////20,30,40,60,10,50,70

	system("pause");
	return EXIT_SUCCESS;
}